package com.nariano.server.utils.mail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import com.nariano.server.domain.entities.User;
import com.nariano.server.services.userservice.UserService;


public class MailSender {
	
	private String messageTemplate;

	private String subject;

	private String from;
	
	public void setFrom(String from) {
		this.from = from;
	}
	
	public void setMessageTemplate(String messageTemplate) {
		this.messageTemplate = messageTemplate.replace("\\n", "\n");
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}



	@Autowired
	@Qualifier(value = "javaMailSender")
	JavaMailSender sender;
	
	@Autowired
	@Qualifier(value = "userService")
	UserService userService;

	public void send(String to) {
		
		User user = userService.findUserFromEmail(to);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(user.getEmail());
        message.setSubject(subject);
        message.setText(String.format(messageTemplate,user.getUserName(), user.getPasswordHashed()));
        sender.send(message);
	}
	
}

